using System;
using System.Collections.Generic;
using System.Text;
using PetShop.Domain.Model;
using PetShop.Infrastructure.NHibernate;
using PetShop.Domain.Services;
using NHibernate;

namespace PetShop.Application.Facade
{
	public class AccountFC
	{
        public Account GetAccountIfExists(string userId, string password)
        {


            if (string.IsNullOrEmpty(userId) || string.IsNullOrEmpty(password))
                return null;


            AccountService svc = new AccountService();
            return svc.GetAccount(userId, password);

   
        }

        public void CreateNewAccount(Account account)
        {

            if (string.IsNullOrEmpty(account.UserId))
                return;


            ISession session = NHibernateHttpModule.CurrentSession;
            ITransaction tx = session.BeginTransaction();

            try
            {

                AccountService svc = new AccountService();
                svc.RegisterAccount(account);
                tx.Commit();

            }
            catch (Exception)
            {
                if (tx != null) tx.Rollback();
                throw;
            }
            finally
            {
                if (tx != null) tx.Dispose();
            }


        }

        public void UpdateAccount(Account account)
        {


            if (account==null)
                return;


            ISession session = NHibernateHttpModule.CurrentSession;
            ITransaction tx = session.BeginTransaction();

            try
            {

                AccountService svc = new AccountService();
                svc.UpdateInfo(account);
                tx.Commit();

            }
            catch (Exception)
            {
                if (tx != null) tx.Rollback();
                throw;
            }
            finally
            {
                if (tx != null) tx.Dispose();
            }



        }

    
	}
}
